<template>
  <div class="login_container">
    <img src="../assets/3.gif" style="width:100%;height:100%"/>
     <div class="login_box">
       <!-- 头像区域 -->
        <div class="avatar_box">
          <img src="../assets/1.jpg" alt="">
        </div>
        <el-button style="margin-top:30px;margin-left:15px" type="danger" icon="el-icon-s-home" round @click="backwelcome()"></el-button>
        <!-- 登录表单区域 -->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="80px" class="login_form" 
                  v-loading="loading"
                  element-loading-text="正在登陆。。。。"
                  element-loading-spinner="el-icon-loading"
                  element-loading-background="rgba(0, 0, 0, 0.8)">
          <!-- 用户名 -->
          <el-form-item prop="username" label="账 号：">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password" label="密  码：">
            <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item prop="captcha" label="验证码：">
            <el-input v-model="loginForm.captcha" class="login_yzm" ></el-input>
            <el-image :src="captchaImg" @click="updatacaptcha" class="yzm_image"></el-image>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>

</template>

<script>
// import {postRequest} from '../utils/api'
export default {
  data () {
    return {
    // 这是登陆表单的数据绑定
      loginForm: {
        username: '',
        password: '',
        captcha: '',
      },
      loading:false,
      // 这是表单的验证规则
      loginFormRules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      captchaImg: '/captcha?time=' + new Date()
    }
  },
  // 点击重置按钮，重置登陆表单
  methods: {
    updatacaptcha () {
      this.captchaImg = '/captcha?time=' + new Date()
    },
    resetLoginForm () {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.postRequest('/login', this.loginForm).then(resp => {
            // alert(JSON.stringify(resp))
            if (resp) {
              this.loading = false
              //存储用户token
              const tokenStr = resp.data.tokenHead+resp.data.token
              window.sessionStorage.setItem('tokenStr',tokenStr)
              //根据用户权限等级跳转页面
              if (resp.data.usergrade==0){
              this.$router.replace("/puser")
            }else if (resp.data.usergrade==1){
              this.$router.replace("/guser")
            }else if (resp.data.usergrade==2){
              this.$router.replace("/zfuser")
            }else{
              this.$router.replace("/xtuser")
            }
            // console.log(resp)
            }
          })
        } else {
          this.$message.console.error('请输入所有信息！')
          return false
        }
      })
    },

    backwelcome() {
      this.$router.replace("/welcome")
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #3792c7;
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px;
}

.login_box {
  width: 450px;
  height: 350px;
  //background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid #31acf4;

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #ffffff;
    border-radius: 80%;
    padding: 10px;
    box-shadow: 0 0 10px #3792c7;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      //background-color: aqua;
    }
  }
}

.login_form {
  top: 25%;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 8%;
  box-sizing: border-box;

}

.login_yzm {
  //position: absolute;
  float: left;
  width: 65%;
}

.yzm_image {
  //padding-left: 80%;
  float: right;
  border-radius: 3px;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
